package com.study.kmp;

import com.study.jvm.GCTest;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;

public class KmpSearch {


    public static void main(String[] args) {

        GCTest test1 = new GCTest(1000);
        GCTest test2 = new GCTest(5000);
        GCTest test3 = new GCTest(10000);
        ExecutorService executorService = new ThreadPoolExecutor(5, 500, 2000, TimeUnit.HOURS, new LinkedBlockingDeque<>(99500));

        FutureTask<Integer> gcTestFutureTask1 = new FutureTask<Integer>(test1);
        FutureTask<Integer> gcTestFutureTask2 = new FutureTask<Integer>(test2);
        FutureTask<Integer> gcTestFutureTask3 = new FutureTask<Integer>(test3);
        executorService.execute(gcTestFutureTask1);
        executorService.execute(gcTestFutureTask1);
        executorService.execute(gcTestFutureTask1);

        new Thread(() -> {
            try {
                int i = gcTestFutureTask1.get();
                if (i==1){
                    System.exit(0);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }).start();

        new Thread(() -> {
            try {
                int i =gcTestFutureTask2.get();
                if (i==1){
                    System.exit(0);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }).start();

        new Thread(() -> {
            try {
                int i =gcTestFutureTask3.get();
                if (i==1){
                    System.exit(0);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }).start();


        // gcTestFutureTask.get();


        //gcTestFutureTask.get();

        Map map = new HashMap();

//        map.put(test, 1);
//
//        test = null;
//
//        System.gc();
//
//        System.out.println(test);

        ThreadLocal threadLocal = new ThreadLocal();
        Thread t = Thread.currentThread();

        threadLocal.set("1");
        threadLocal.set("2");
        String s = (String) threadLocal.get();

        System.out.println(t);
        System.out.println(s);

    }

    public static int kmpSearch(String str1, String str2) {
        if (str1 == null && str2 == null || str2.length() > str1.length()) {
            return -1;
        }

        char[] chars1 = str1.toCharArray();
        char[] chars2 = str2.toCharArray();

        int strIndex1 = 0;
        int strIndex2 = 0;
        int nextArray[] = getNextArray(str2);
        while (strIndex1 < str1.length() && strIndex2 < str2.length()) {
            if (chars1[strIndex1] == chars2[strIndex2]) {
                strIndex1++;
                strIndex2++;
            } else if (nextArray[strIndex2] == -1) {
                strIndex1++;
            } else {
                // 没有最大前后缀匹配长度 相当于steIndex2回零
                strIndex2 = nextArray[strIndex2];
            }
        }

        return strIndex1 = strIndex2 == str2.length() ? strIndex1 - strIndex2 : -1;
    }


    public static int[] getNextArray(String str) {
        if (str == null || str.length() == 1) {
            return new int[]{-1};
        }
        char[] chars = str.toCharArray();
        int[] nextArray = new int[str.length()];
        nextArray[0] = -1;
        nextArray[1] = 0;
        int i = 2;
        //cn =i -2;
        // cn代表最长前后缀匹配长度 与i-1位置比较的字符的位子
        int cn = 0;
        while (i < str.length()) {//chars[nextArray[cn]] == chars[i - 1]
            if (chars[cn] == chars[i - 1]) {
                //nextArray[i++] = ++nextArray[cn];
                nextArray[i] = ++cn;
                i++;
            } else if (cn > 0) {
                cn = nextArray[cn];
            } else {
                nextArray[i] = 0;
                i++;
            }
        }
        return nextArray;
    }

}
